﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using Silvermoon.Animations;
using Silvermoon.Visuals;
using System.Reflection;
using System.Drawing;
using Silvermoon.Controls.Classes;
using System.IO;
using Silvermoon.Images;
using Silvermoon.Utils;
using Silvermoon.OpenGL;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Standard toolbar with left and right button and a middle button to access the keyboard.
    /// </summary>
    public class Toolbar : ToolbarBase
    {
        const int buttonHeight = 64;
        #region fields

        private Button leftButton;
        private Button rightButton;
        private ContextMenu leftMenu;
        private ContextMenu rightMenu;
        private ImageButton keyboardButton;
        private static Texture tex1, tex2;
        private static bool initialized = false;
        private Control[] subControls;
        private Color color = SysColors.ButtonColor;


        #endregion
        #region ctor

        public Toolbar()
            : base()
        {
            InitializeStatics();
            leftButton = new Button { Color = SysColors.ButtonColor, Enabled = false, Style = RenderStyle.Chrome };
            rightButton = new Button { Color = SysColors.ButtonColor, Enabled = false, Style = RenderStyle.Chrome };

            leftButton.TextChanged += OnButtonTextChanged;
            rightButton.TextChanged += OnButtonTextChanged;
            rightButton.PressedChanged += OnButtonPressedChanged;
            leftButton.PressedChanged += OnButtonPressedChanged;
            leftButton.DownChanged += OnButtonPressedChanged;
            rightButton.DownChanged += OnButtonPressedChanged;
            keyboardButton = new ImageButton
            {
                Size = new Size(92, 92),
                Image = tex1,
                PressedImage = tex2,
                Toggle = true,
                Color = SysColors.SysKeyboardColor,
                Icon = SysImages.Keyboard,
                IconColor = ColorUtil.AlphaColor(Color.White, 240)

            };
            keyboardButton.DownChanged += new EventHandler(OnKeyboardBtnDownChanged);
            keyboardButton.Flags = ControlFlags.IgnoreClipping;
            subControls = new Control[] { leftButton, rightButton, keyboardButton };
            Height = buttonHeight;
        }


        #endregion
        #region props

        protected internal override IList<Control> SubControls { get { return subControls; } }

        /// <summary>
        /// Gets the left toolbar button.
        /// </summary>
        public Button LeftButton { get { return leftButton; } }

        /// <summary>
        /// Gets the right toolbar button.
        /// </summary>
        public Button RightButton { get { return rightButton; } }

        public ContextMenu LeftMenu { get { return leftMenu; } set { leftMenu = value; SetMenu(leftButton, value); } }

        public ContextMenu RightMenu { get { return rightMenu; } set { rightMenu = value; SetMenu(rightButton, value); } }

        public Color Color
        {
            get { return color; }
            set
            {
                if (color != value)
                {
                    color = value;
                    Invalidate(InvalidationFlag.Color);
                }
            }
        }

        protected override int OverlaySize { get { return 10; } }

        #endregion
        #region methods

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Color))
            {
                leftButton.Color = color;
                rightButton.Color = color;
                keyboardButton.Color = ColorUtil.AlphaBlendColor(color, Color.White, 128);
            }
            if (CheckFlag(InvalidationFlag.LayoutAppearance | InvalidationFlag.Size | InvalidationFlag.Margin))
            {
                leftButton.Bounds = new Rectangle(-2, Height - buttonHeight, Width / 2 + 4, buttonHeight + 8);
                rightButton.Bounds = new Rectangle(Width / 2, Height - buttonHeight, Width / 2 + 4, buttonHeight + 8);
                keyboardButton.Location = new Point((Width - keyboardButton.Width) / 2, Height - keyboardButton.Height + 8);
                keyboardButton.Visible = true;
            }
            base.OnLayout();
        }

        protected override void OnSizeChanged()
        {
            if (Height != buttonHeight) Height = buttonHeight; else base.OnSizeChanged();
        }

        private static void InitializeStatics()
        {
            if (initialized) return;
            tex1 = new Texture();
            tex2 = new Texture();
            Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Silvermoon.Images.MiddleSys.png");
            tex1.LoadFromStream(s);
            s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Silvermoon.Images.MiddleSysPressed.png");
            tex2.LoadFromStream(s);

            initialized = true;
        }

        void OnButtonTextChanged(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (!string.IsNullOrEmpty(b.Text)) b.Enabled = true;
        }

        void OnButtonPressedChanged(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            b.Color = b.IsDownOrPressed ? Color.FromArgb(20, 172, 255) : color;
        }

        void OnKeyboardBtnDownChanged(object sender, EventArgs e)
        {
            ImageButton b = sender as ImageButton;
            if (Window != null) Window.ShowKeyboard = b.IsDown;
        }

        private void SetMenu(Button button, ContextMenu menu)
        {
            button.Toggle = menu != null;
            if (menu != null)
            {
                button.DownChanged += (s, e) => { if (button.IsDown) menu.Show(button); else menu.Hide(TransitionMode.FadeOut | TransitionMode.SwipeOutToBottom); };
                menu.Showing += (s, e) => button.IsDown = true;
                menu.Hidden += (s, e) => button.IsDown = false;
            }
        }

        protected override void OnWindowChanging(Window oldValue, Window newValue)
        {
            if (oldValue != null)
            {
                oldValue.KeyDown -= OnWindowKeyDown;
                oldValue.ShowKeyboardChanged -= OnShowKeyboard;
            }
            if (newValue != null)
            {
                newValue.ShowKeyboardChanged += OnShowKeyboard;
                newValue.KeyDown += OnWindowKeyDown;
            }
            base.OnWindowChanging(oldValue, newValue);
        }

        protected virtual void OnWindowKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (!e.Handled)
            {
                switch (e.KeyCode)
                {
                    case System.Windows.Forms.Keys.F1:
                        LeftButton.PerformClick();
                        break;

                    case System.Windows.Forms.Keys.F2:
                        RightButton.PerformClick();
                        break;
                }
                e.Handled = true;
            }
        }

        protected virtual void OnShowKeyboard(object sender, EventArgs e)
        {
            ImageButton b = keyboardButton;
            b.IsDown = Window.ShowKeyboard;
            if (Window.ShowKeyboard)
            {
                b.Color = Color.FromArgb(40, 192, 255);
            }
            else
            {
                b.Color = ColorUtil.AlphaBlendColor(color, Color.White, 128);
            }
        }

        #endregion
    }
}
